/*
 * pwm.c
 *
 *  Created on: 02-04-2012
 *      Author: Mateusz
 */

#include "LPC13xx.h"

//CT32B1 for PWM
void PWM_INIT(void) {

	//Wlaczenie macroceli od CT32B1
	LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 10);

	//Ustawinie preskalera
	LPC_TMR32B1->PR = SystemCoreClock/6120000; //6120000/255 -> PWM=24kHz

	//Ustawienie pinow od PWM
	LPC_IOCON->R_PIO1_1 |= 0b011;
	LPC_IOCON->R_PIO1_2 |= 0b011;
	LPC_IOCON->ARM_SWDIO_PIO1_3 |= 0b011;

	//Wyłączenie pullupów
	LPC_IOCON->R_PIO1_1 &= ~(3<<3);
	LPC_IOCON->R_PIO1_2 &= ~(3<<3);
	LPC_IOCON->ARM_SWDIO_PIO1_3 &= ~(3<<3);

	//MR3 jako ograniczenie rozdzielczości PWM //RESET ON MATCH
	LPC_TMR32B1->MCR = (1 << 10);
	LPC_TMR32B1->MR3 = 255;
	LPC_TMR32B1->MR0 = 256;
	LPC_TMR32B1->MR1 = 256;
	LPC_TMR32B1->MR2 = 256;

	//MAT0, MAT1 i MAT2 jako wyjscie PWM
	LPC_TMR32B1->PWMC = (1 << 2) | (1 << 1) | (1 << 0);

	//Wlaczenie timera
	LPC_TMR32B1->TCR = 1;

}


void PWM(uint8_t R, uint8_t G, uint8_t B) {
	LPC_TMR32B1->MR0 = 256 - R;
	LPC_TMR32B1->MR1 = 256 - G;
	LPC_TMR32B1->MR2 = 256 - B;
}
